package com.example.player.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.easefun.polyvsdk.RestVO;
import com.easefun.polyvsdk.vo.PolyvRestVO;
import com.easefun.polyvsdk.vo.PolyvVideoVO;
import com.example.player.PolyvDownloadSQLiteHelper;
import com.example.player.R;
import com.example.player.activity.PlayerActivity;

import java.util.List;

public class OnlineListViewAdapter extends AbsRecyclerViewAdapter {
    private static final String TAG = OnlineListViewAdapter.class.getSimpleName();
    private Context context;
    private List<RestVO> videos;
    //private static PolyvDownloadSQLiteHelper downloadSQLiteHelper;

    public OnlineListViewAdapter(RecyclerView recyclerView, Context context, List<RestVO> videos){
        super(recyclerView);
        this.context=context;
        this.videos=videos;
        //downloadSQLiteHelper=PolyvDownloadSQLiteHelper.getInstance(context);
    }
    private class ItemViewHolder extends AbsRecyclerViewAdapter.ClickableViewHolder{
        ImageView iv_cover;
        TextView tv_title,tv_time,tv_play;

        ItemViewHolder(View itemView) {
            super(itemView);
            iv_cover=$(R.id.iv_cover);
            tv_title=$(R.id.tv_title);
            tv_time=$(R.id.tv_time);
            tv_play=$(R.id.tv_play);
        }
    }

    @Override
    public ClickableViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        bindContext(parent.getContext());
        return new ItemViewHolder(LayoutInflater.from(getContext())
                .inflate(R.layout.listview_online_item,parent,false));
    }

    @Override
    public void onBindViewHolder(ClickableViewHolder holder,int position) {
        if (holder instanceof ItemViewHolder) {
            ItemViewHolder mHolder = (ItemViewHolder) holder;
            RestVO restVO = videos.get(position);
            mHolder.tv_title.setText(restVO.getTitle());
            mHolder.tv_time.setText(restVO.getDuration());
            mHolder.tv_play.setOnClickListener(new PlayListener(restVO.getVid()));
        }
        super.onBindViewHolder(holder, position);
    }

    @Override
    public int getItemCount() { return videos.size(); }

    private class PlayListener implements View.OnClickListener {
        private String vid;

        PlayListener(String vid){
            this.vid=vid;
        }

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(context,PlayerActivity.class);
            intent.putExtra("playMode", PolyvVideoVO.MODE_VIDEO);
            intent.putExtra("vid",vid);
            intent.putExtra("bitrate",0);
            intent.putExtra("startNow",false);
            intent.putExtra("isMustFromLocal",false);
//            intent.putExtra("fileType",)
            context.startActivity(intent);
        }
    }


}
